<?php

/* Author: tbloc@cantho.gov.vn */

class Sys_func_group {

//Properties
    private $SFG_ID;
    private $SFG_NAME;
    private $SFG_ORDER;
    private $IS_ACTIVE;

//properties set get 
    public function setSFG_ID($SFG_ID) {
        $this->SFG_ID = $SFG_ID;
    }

    public function getSFG_ID() {
        return $this->SFG_ID;
    }

    public function setSFG_NAME($SFG_NAME) {
        $this->SFG_NAME = $SFG_NAME;
    }

    public function getSFG_NAME() {
        return $this->SFG_NAME;
    }

    public function setSFG_ORDER($SFG_ORDER) {
        $this->SFG_ORDER = $SFG_ORDER;
    }

    public function getSFG_ORDER() {
        return $this->SFG_ORDER;
    }

    public function setIS_ACTIVE($IS_ACTIVE) {
        $this->IS_ACTIVE = $IS_ACTIVE;
    }

    public function getIS_ACTIVE() {
        return $this->IS_ACTIVE;
    }

//crud methods
    public function insert($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `sys_func_group`(                        
			`SFG_ID`, 
				`SFG_NAME`, 
				`SFG_ORDER`, 
				`IS_ACTIVE` 
		)
	VALUES(
    		`SFG_ID`, 
	    		`SFG_NAME`, 
	    		`SFG_ORDER`, 
	    		`IS_ACTIVE` 
	    );";
            $parameters = array(
                `:SFG_ID`,
                `:SFG_NAME`,
                `:SFG_ORDER`,
                `:IS_ACTIVE`
            );
            $values = array(
                $this->getSFG_ID(),
                $this->getSFG_NAME(),
                $this->getSFG_ORDER(),
                $this->getIS_ACTIVE()
            );
            $types = array(
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
            );
            $lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
            $this->setSFG_ID($lastInsertID);
        }
    }

    public function delete($db = null) {
        if ($db != null) {
            $sql = "update `sys_func_group`
	                set `is_active`=0
	                where `SFG_ID` = :SFG_ID;";
            $parameters = array(":SFG_ID");
            $values = array($this->getSFG_ID());
            $types = array(PDO::PARAM_INT); //Kiem tra lai kieu du lieu nhe
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function update($db = null) {
        if ($db != null) {
            $sql = "UPDATE `sys_func_group` SET 
			`SFG_ID`=:SFG_ID,
				`SFG_NAME`=:SFG_NAME,
				`SFG_ORDER`=:SFG_ORDER,
				`IS_ACTIVE`=:IS_ACTIVE
			WHERE `SFG_ID` = :SFG_ID;";
            $parameters = array(
                ":SFG_ID",
                ":SFG_NAME",
                ":SFG_ORDER",
                ":IS_ACTIVE"
            );
            // Kiem tra dau , nhe
            $values = array(
                $this->getSFG_ID(),
                $this->getSFG_NAME(),
                $this->getSFG_ORDER(),
                $this->getIS_ACTIVE()
            );
            $types = array(
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_STR, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
                PDO::PARAM_INT, //Kiem tra lai kieu du lieu nhe
            );
            $result = $db->executeNonQuery($sql, $parameters, $values, $types);
            return $result;
        }
    }

    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`SFG_ID`=:SFG_ID,
				`SFG_NAME`=:SFG_NAME,
				`SFG_ORDER`=:SFG_ORDER,
				`IS_ACTIVE`=:IS_ACTIVE
			FROM `sys_func_group` WHERE `SFG_ID` = :SFG_ID and `is_active`=1;";
            $parameters = array(":SFG_ID");
            $values = array($this->getSFG_ID());
            $types = array(PDO::PARAM_INT);
            $rows = $db->executeQuery($sql, $parameters, $values, $types);
            if ($rows != null) {
                $row = $rows->fetch();
                $this->setSFG_ID($row->SFG_ID);
                $this->setSFG_NAME($row->SFG_NAME);
                $this->setSFG_ORDER($row->SFG_ORDER);
                $this->setIS_ACTIVE($row->IS_ACTIVE);
            }
            return $this;
        }
    }

    public function getList($db = null) {
        if ($db != null) {
            $sql = "SELECT
			`SFG_ID`,
				`SFG_NAME`,
				`SFG_ORDER`,
				`IS_ACTIVE`
			FROM `sys_func_group` WHERE `is_active`=1 ORDER BY SFG_ORDER ASC;";
            $rows = $db->executeQuery($sql);            
            $objects = array();
            if ($rows != null) {
                while ($row = $rows->fetch()) {
                    $obj = new Sys_func_group();
                    $obj->setSFG_ID($row->SFG_ID);
                    $obj->setSFG_NAME($row->SFG_NAME);
                    $obj->setSFG_ORDER($row->SFG_ORDER);
                    $obj->setIS_ACTIVE($row->IS_ACTIVE);
				
                    $objects[] = $obj;
                }
                return $objects;
            }           
        }
    }

}
?>

